package com.github.wangmingchang.sqltransformbean.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author wangmingchang
 * @date 2022/10/26 15:27
 */
public class FileExportUtil {

    private static int num = 0;

    private static String root = "D:\\idea-repository\\wangmingchang\\sqltransformbean";
    private static String newRoot = "D:\\idea-repository\\my\\sqltransformbean";
    private static String myRoot = "I:\\idea-item\\gitee\\sqltransformbean";

    public static void main(String[] args) throws Exception {
//        readAllFile(root);
        recoverFile(myRoot);
        deleteTxtFile(myRoot);
        System.out.println("总共文件数："+num);
    }
    /**
     * 删除txt文件
     * @param filepath
     */
    private static void deleteTxtFile(String filepath) {
        File file = new File(filepath);
        if (!file.isDirectory()) {
            String path = file.getPath();
            if(path.endsWith(".txt")){
                System.out.println("文件："+path);
                file.deleteOnExit();
                num++;
            }
        } else if (file.isDirectory()) {
            String path = file.getPath();
            if(path.contains(".git") || path.contains(".idea") || path.contains(".mvn") || path.contains("uml")
                    || path.contains("target")){
                return;
            }
            System.out.println("文件夹:"+path);
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(filepath);
                if (!readfile.isDirectory()) {
                    String path2 = readfile.getPath();
                    System.out.println("文件2："+path2);
                } else if (readfile.isDirectory()) {
                    deleteTxtFile(filepath + "\\" + filelist[i]);//递归
                }
            }
        }

    }

    /**
     * 恢复java文件
     * @param filepath
     * @throws Exception
     */
    public static void recoverFile(String filepath)throws Exception{
        File file = new File(filepath);
        if (!file.isDirectory()) {
            String path = file.getPath();
            if(path.endsWith(".txt")){
                System.out.println("文件："+path);
                String name = file.getName();
                String newPath = file.getParent() +"/" + name.substring(0,name.length()-4);
                System.out.println("文件new:"+newPath);
                File newFile = new File(newPath);
                copyFileUsingFileChannels(file, newFile);
                num++;
            }
        } else if (file.isDirectory()) {
            String path = file.getPath();
            if(path.contains(".git") || path.contains(".idea") || path.contains(".mvn") || path.contains("uml")
                    || path.contains("target")){
                return;
            }
            System.out.println("文件夹:"+path);
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(filepath);
                if (!readfile.isDirectory()) {
                    String path2 = readfile.getPath();
                    System.out.println("文件2："+path2);
                } else if (readfile.isDirectory()) {
                    recoverFile(filepath + "\\" + filelist[i]);//递归
                }
            }
        }
    }


    /**
     * 复制文件
     * @param filepath
     * @throws Exception
     */
    public static void readAllFile(String filepath) throws Exception {
        File file = new File(filepath);
        if (!file.isDirectory()) {
            String path = file.getPath();
            if(path.endsWith(".iml") || path.endsWith(".jar")){
                return;
            }

            System.out.println("文件："+path);
            String newPath = newRoot + path.substring(root.length());
            if(path.endsWith(".java") || path.endsWith(".xml") || path.endsWith(".html") || path.endsWith(".properties")){
                newPath += ".txt";
            }
            System.out.println("文件new:"+newPath);
            File newFile = new File(newPath);
            if(!newFile.exists()){
                boolean isSuccess = newFile.createNewFile();
                System.out.println("创建文件成功:"+newPath);
            }
            copyFileUsingFileChannels(file, newFile);
            num++;
        } else if (file.isDirectory()) {
            String path = file.getPath();
            if(path.contains(".git") || path.contains(".idea") || path.contains(".mvn") || path.contains("uml")
                    || path.contains("target")){
                return;
            }
            System.out.println("文件夹:"+path);
            String newPath = newRoot + path.substring(root.length());
            System.out.println("文件夹new:"+newPath);
            File newFile = new File(newPath);
            if(!newFile.exists()){
                // 创建文件夹
                newFile.mkdirs();
            }

            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(filepath);
                if (!readfile.isDirectory()) {
                    String path2 = readfile.getPath();
                    System.out.println("文件2："+path2);
                    num++;
                } else if (readfile.isDirectory()) {
                    readAllFile(filepath + "\\" + filelist[i]);//递归
                }
            }
        }
    }


    private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }
}
